package com.example.nbastar;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class List_item extends AppCompatActivity {
    private static final String TAG = "List_item";
    ListView mLvBall;
    BaseAdapter adapter;
   // List<Bean.Ball> ballList;
    List<Bean.Ball>ballList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_lanqiu);

        initView();
        initData();
    }
    private void initData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://192.168.43.21:8080/nbaserver"+"/Ball/list")
                .get()
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.e(TAG, "onFailure: "+ e);
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //获取json字符串数据，response.body().string()
                String jsonStr = response.body().string();
                Log.e(TAG, "onResponse: "+ jsonStr);
                //解析
                Gson gson = new Gson();
                Bean bean = gson.fromJson(jsonStr, Bean.class);
                //更新数据
                ballList = bean.getList();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    private void initView() {
        mLvBall = findViewById(R.id.lv_ball);
        mLvBall.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(List_item.this,List_item_ball.class);
                intent.putExtra("ball",ballList.get(position));
                startActivity(intent);
            }
        });

        ballList = new ArrayList<>();
        adapter = new MyAdapter();
        mLvBall.setAdapter(adapter);
    }


    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return ballList.size();
        }

        @Override
        public Object getItem(int position) {
            return ballList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return ballList.get(position).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = View.inflate(List_item.this
                        ,R.layout.item_ball,null);

                ViewHolder viewHolder = new ViewHolder();
                viewHolder.tvBallName = convertView.findViewById(R.id.tv_ball_name);
                viewHolder.ivBallImage = convertView.findViewById(R.id.iv_ball_img);
                convertView.setTag(viewHolder);
            }
            ViewHolder vh = (ViewHolder) convertView.getTag();
            vh.tvBallName.setText(ballList.get(position).getName());
            //加载图片
            Picasso.with(List_item.this)
                    .load("http://192.168.43.21:8080/nbaserver/"+ballList.get(position).getImg())
                    .resize(200,150)
                    .centerCrop()
                    .into(vh.ivBallImage);
            return convertView;
        }

        class ViewHolder{
           TextView tvBallName;
           ImageView ivBallImage;
        }
    }
}
